package healthsolutions.wk;

import healthsolution.wk.R;

import datamanagement.UserChoser;

import android.app.Activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;
//import positioncysand.wk.HTService;


public class LoginActivity extends Activity {

	 public static String TAG = "LoginActivity:";
	//private TelephonyManager mTelManager = (TelephonyManager)this.getSystemService(TELEPHONY_SERVICE);

	/** Called when the activity is first created. */
	/*  
	 private String pathFiles="/CYS/";	 
    private String strNameFileTime="cystime.txt";
	private Files objFiles = new Files();
    */

	  
	@Override
	public void onCreate(Bundle savedInstanceState) 
	{
		Log.i(TAG,"[onCreate] Dentro OnCreate");
	
	//	SharedPreferences settings = getSharedPreferences("HS",MODE_PRIVATE);			//DEBUG
			
		super.onCreate(savedInstanceState);	
	    setContentView(R.layout.login);    

	    Log.i(TAG,"[onCreate] Dentro Content");
	    
	    final EditText etxLogin = (EditText) findViewById(R.id.LoginPin);
	    
	    Button btLoginPin = (Button) findViewById(R.id.btLogin);
	    
	    btLoginPin.setOnClickListener(new OnClickListener()
	    {     
			public void onClick(View v) 
			{				
				Log.i(TAG,"[onCreate] Dentro OnClick");
				
			    ////////////////////////////// TIMER SENDER //////////////////////////////
			    	String strTextLogin=etxLogin.getText().toString();
					
			    	Log.d(TAG,"[onCreate] strTEXT: "+strTextLogin);
			    	
					if((strTextLogin!=null)&&(strTextLogin!="")&&(strTextLogin!=" ")&&(strTextLogin.length()==8))			
					{
						
						byte[] byUserCard=new byte[1];
						
						strTextLogin.getBytes(0,1,byUserCard , 0);	//DEBUG	//FD
						
						if(byUserCard[0]==0x31)
						{
							SharedPreferences settings = getSharedPreferences("HS",MODE_PRIVATE);			   
							SharedPreferences.Editor editor = settings.edit();
						    			    
						    //	Toast.makeText(appContext,"Mail ingresado: "+mail.getText().toString(),Toast.LENGTH_SHORT).show();
								  
						    editor.putString("HS_Start", "OK" );
						    editor.putString("StartService", "OK" );
						    editor.putString("UserKind", "M" );
						    editor.commit();
						    
						    Log.d(TAG,"[onCreate] Tipo de Usuario: "+"M");
 
						}else if(byUserCard[0]==0x32)
						{
							SharedPreferences settings = getSharedPreferences("HS",MODE_PRIVATE);			   
							SharedPreferences.Editor editor = settings.edit();
						    			    
						    //	Toast.makeText(appContext,"Mail ingresado: "+mail.getText().toString(),Toast.LENGTH_SHORT).show();
								  
						    editor.putString("HS_Start", "OK" );
						    editor.putString("StartService", "OK" );
						    editor.putString("UserKind", "P" );
						    
						    editor.commit();
						    Log.d(TAG,"[onCreate] Tipo de Usuario: "+"P");
						   
						}else
						{
							
						}
						
						 Intent IntWelcome = null;
						    Log.i(TAG,"[onCreate] Voy a empezar activity UserChoser. ");
						    
						    try
						    {
						    	IntWelcome=new Intent(getApplicationContext(),UserChoser.class);
						    }catch(Exception Ex)
						    {
						    	Log.e(TAG,"[onCreate] Exception Particular Intent: "+Ex);	
						    }
						    try
							{
						    	startActivity(IntWelcome);
							}catch(Exception ex)
							{
								Log.i(TAG,"[onCreate] Exception Particular Start: "+ex);	
							}
						    
					}else
					{	Toast.makeText(getApplicationContext(),"ERROR - Ingresar Pin Correcto",Toast.LENGTH_SHORT).show();
					
					
					}	
			    			
			}
	    });
	 
	    //////////////////////////////////// MINIMIZAR /////////////////////////////////////////
		ImageButton btMinimizes = (ImageButton) findViewById(R.id.iconminimize_lg);    	    
    	btMinimizes.setOnClickListener(new OnClickListener()
	    {     
			public void onClick(View v) 
			{				
				voMinimizarApp();    				
			}
	    });
	    
	}	
	
	// PARA QUE NO PUEDA REGRESAR A LA PANTALLA ANTERIOR
	@Override
	public void onBackPressed() {
		Log.i(TAG, "[onBackPressed] onBackPressed");
		voMinimizarApp();	
	}
	
	public void voMinimizarApp()
	{
		try
		{
			Intent startMain = new Intent(Intent.ACTION_MAIN);
	    	startMain.addCategory(Intent.CATEGORY_HOME);
	    	startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
	    	startActivity(startMain);
	    	
		}catch(Exception ex)
		{
			Log.i(TAG, "[voMinimizarApp] Exception: "+ex);
		}
		
	}
	
}